
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 5 (IND) ****************************
*************** THIS DO FILE GENERATES THE VALUE FUNCTION **********************
********************************************************************************
********************************************************************************

*Version: 2023 March 29


cap mkdir `"$output/temp"'
cap mkdir `"$output/log/relax_IND_5_value_function_computation"'

	
cap log close 
log using `"$output/log/relax_IND_5_value_function_computation/relax_IND_5_value_function_computation.smcl"', replace 

di "This run uses code version from 2023/03/29"

******************************************
*** Generate joint denial probabilites ***
******************************************
local max_proc_n = 5

use `"$output/data/$line_file"', clear

*select sample 
keep if Y_0 == 0				 				// Keep if first submission is denied 

gegen bin_C = cut(line_item_value) , at(0(25)250)	
replace bin_C = 250 if bin_C == . 	
	
*merge in denial probabilites calculated in step 1 
merge m:1 state pay_type_pooled visit_code_cat bin_C size_indicator using  `"$output/temp/mean_denial_probabilities_bin.dta"'
keep if _merge == 3 
drop _merge 


*generate line item identifier within visits 
sort visit_billing_id proc1
by visit_billing_id: gen proc_id = _n	
by visit_billing_id: gen proc_n = _N	

//keep only those visits for which one line item is denied 
keep if proc_n == 1 

*create the necessary variables 
foreach v of new S {
	gen `v'_1 = .
	gen `v'_2 = .
	gen `v'_1_2 = .
	gen `v'_3 = .
	gen `v'_1_3 = .
	gen `v'_2_3 = .
	gen `v'_1_2_3 = .
	gen `v'_4 = .
	gen `v'_1_4 = .
	gen `v'_2_4 = .
	gen `v'_3_4 = .
	gen `v'_1_2_4 = .
	gen `v'_1_3_4 = .
	gen `v'_2_3_4 = .
	gen `v'_1_2_3_4 = .
	gen `v'_5 = .
	gen `v'_1_5 = .
	gen `v'_2_5 = .
	gen `v'_3_5 = .
	gen `v'_4_5 = .
	gen `v'_1_2_5 = .
	gen `v'_1_3_5 = .
	gen `v'_1_4_5 = .
	gen `v'_2_3_5 = .
	gen `v'_2_4_5 = .
	gen `v'_3_4_5 = .
	gen `v'_1_2_3_5 = .
	gen `v'_1_2_4_5 = .
	gen `v'_1_3_4_5 = .
	gen `v'_2_3_4_5 = .
	gen `v'_1_2_3_4_5 = .
	gen `v'_no_denial = .

}


*Calculate joint denial probabilities
*for 5 line items in a visit
* no denial in a visit 
sort visit_billing_id proc_id
by visit_billing_id: replace S_no_denial = (1-p_denial[1]) * (1-p_denial[2]) * (1-p_denial[3]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5

*first procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_1 = p_denial[1] * (1-p_denial[2]) * (1-p_denial[3]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5
	
*second procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_2 = p_denial[2] * (1-p_denial[1]) * (1-p_denial[3]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5
				
*first and second procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_2 = p_denial[1] * p_denial[2] * (1-p_denial[3]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5
							
*third procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_3 = p_denial[3] * (1-p_denial[2]) * (1-p_denial[1]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5

*first and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3 = p_denial[1] * p_denial[3] * (1-p_denial[2]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5
			 
*second and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3 = p_denial[2] * p_denial[3] * (1-p_denial[1]) * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5
 
*first, second and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3 = p_denial[1] * p_denial[2] * p_denial[3] * (1-p_denial[4]) * (1-p_denial[5]) if proc_n == 5

*fourth procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_4 = p_denial[4] * (1-p_denial[1]) * (1-p_denial[3]) * (1-p_denial[2]) * (1-p_denial[5]) if proc_n == 5	

*first and fourth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_4 = p_denial[1] * p_denial[4] * (1-p_denial[3]) * (1-p_denial[2]) * (1-p_denial[5]) if proc_n == 5			

*second and fourth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_2_4 = p_denial[2] * p_denial[4] * (1-p_denial[3]) * (1-p_denial[1]) * (1-p_denial[5]) if proc_n == 5		

*third and fourth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_3_4 = p_denial[3] * p_denial[4] * (1-p_denial[2]) * (1-p_denial[1]) * (1-p_denial[5]) if proc_n == 5	

*first, second and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_4 = p_denial[1] * p_denial[2] * p_denial[4] * (1-p_denial[3]) * (1-p_denial[5]) if proc_n == 5

*first, third and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3_4 = p_denial[1] * p_denial[3] * p_denial[4] * (1-p_denial[2]) * (1-p_denial[5]) if proc_n == 5

*second, third and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3_4 = p_denial[2] * p_denial[3] * p_denial[4] * (1-p_denial[1]) * (1-p_denial[5]) if proc_n == 5

*first, second, third and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3_4 = p_denial[1] * p_denial[2] * p_denial[3] * p_denial[4] * (1-p_denial[5]) if proc_n == 5

*fifth procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_5 = p_denial[5] * (1-p_denial[1]) * (1-p_denial[3]) * (1-p_denial[2]) * (1-p_denial[4]) if proc_n == 5	 

*first and fifth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_5 = p_denial[1] * p_denial[5] * (1-p_denial[3]) * (1-p_denial[2]) * (1-p_denial[4]) if proc_n == 5

*second and fifth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_2_5 = p_denial[2] * p_denial[5] * (1-p_denial[3]) * (1-p_denial[1]) * (1-p_denial[4]) if proc_n == 5

*third and fifth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_3_5 = p_denial[3] * p_denial[5] * (1-p_denial[2]) * (1-p_denial[1]) * (1-p_denial[4]) if proc_n == 5

*fourth and fifth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_4_5 = p_denial[4] * p_denial[5] * (1-p_denial[2]) * (1-p_denial[1]) * (1-p_denial[3]) if proc_n == 5

*first, second and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_5 = p_denial[1] * p_denial[2] * p_denial[5] * (1-p_denial[3]) * (1-p_denial[4]) if proc_n == 5

*first, third and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3_5 = p_denial[1] * p_denial[3] * p_denial[5] * (1-p_denial[2]) * (1-p_denial[4]) if proc_n == 5

*first, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_4_5 = p_denial[1] * p_denial[4] * p_denial[5] * (1-p_denial[2]) * (1-p_denial[3]) if proc_n == 5

*second, third and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3_5 = p_denial[2] * p_denial[3] * p_denial[5] * (1-p_denial[1]) * (1-p_denial[4]) if proc_n == 5

*second, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_4_5 = p_denial[2] * p_denial[4] * p_denial[5] * (1-p_denial[1]) * (1-p_denial[3]) if proc_n == 5

*third, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_3_4_5 = p_denial[3] * p_denial[4] * p_denial[5] * (1-p_denial[1]) * (1-p_denial[2]) if proc_n == 5

*first, second, third and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3_5 = p_denial[1] * p_denial[2] * p_denial[3] * p_denial[5] * (1-p_denial[4]) if proc_n == 5

*first, second, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_4_5 = p_denial[1] * p_denial[2] * p_denial[4] * p_denial[5] * (1-p_denial[3]) if proc_n == 5

*first, third, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3_4_5 = p_denial[1] * p_denial[3] * p_denial[4] * p_denial[5] * (1-p_denial[2]) if proc_n == 5

*second, third, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3_4_5 = p_denial[2] * p_denial[3] * p_denial[4] * p_denial[5] * (1-p_denial[1]) if proc_n == 5

*first, second, third, fourth and fifth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3_4_5 = p_denial[1] * p_denial[2] * p_denial[3] * p_denial[4] * p_denial[5] if proc_n == 5



*for 4 line items in a visit
* no denial in a visit 
sort visit_billing_id proc_id
by visit_billing_id: replace S_no_denial = (1-p_denial[1]) * (1-p_denial[2]) * (1-p_denial[3]) * (1-p_denial[4]) if proc_n == 4

*first procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_1 = p_denial[1] * (1-p_denial[2]) * (1-p_denial[3]) * (1-p_denial[4]) if proc_n == 4  

*second procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_2 = p_denial[2] * (1-p_denial[1]) * (1-p_denial[3]) * (1-p_denial[4]) if proc_n == 4  
				
*first and second procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_2 = p_denial[1] * p_denial[2] * (1-p_denial[3]) * (1-p_denial[4]) if proc_n == 4  
							
*third procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_3 = p_denial[3] * (1-p_denial[2]) * (1-p_denial[1]) * (1-p_denial[4]) if proc_n == 4  

*first and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3 = p_denial[1] * p_denial[3] * (1-p_denial[2]) * (1-p_denial[4]) if proc_n == 4  
			
*second and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3 = p_denial[2] * p_denial[3] * (1-p_denial[1]) * (1-p_denial[4]) if proc_n == 4  

*first, second and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3 = p_denial[1] * p_denial[2] * p_denial[3] * (1-p_denial[4]) if proc_n == 4  

*fourth procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_4 = p_denial[4] * (1-p_denial[1]) * (1-p_denial[3]) * (1-p_denial[2]) if proc_n == 4 		

*first and fourth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_4 = p_denial[1] * p_denial[4] * (1-p_denial[3]) * (1-p_denial[2]) if proc_n == 4  			

*second and fourth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_2_4 = p_denial[2] * p_denial[4] * (1-p_denial[3]) * (1-p_denial[1]) if proc_n == 4  		

*third and fourth procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_3_4 = p_denial[3] * p_denial[4] * (1-p_denial[2]) * (1-p_denial[1]) if proc_n == 4  	

*first, second and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_4 = p_denial[1] * p_denial[2] * p_denial[4] * (1-p_denial[3]) if proc_n == 4  

*first, third and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3_4 = p_denial[1] * p_denial[3] * p_denial[4] * (1-p_denial[2]) if proc_n == 4  

*second, third and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3_4 = p_denial[2] * p_denial[3] * p_denial[4] * (1-p_denial[1]) if proc_n == 4 

*first, second, third and fourth procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3_4 = p_denial[1] * p_denial[2] * p_denial[3] * p_denial[4] if proc_n == 4 
	

*for 3 line items in a visit
* no denial in a visit 
sort visit_billing_id proc_id
by visit_billing_id: replace S_no_denial = (1-p_denial[1]) * (1-p_denial[2]) * (1-p_denial[3]) if proc_n == 3

*first procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_1 = p_denial[1] * (1-p_denial[2]) * (1-p_denial[3]) if proc_n == 3 

*second procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_2 = p_denial[2] * (1-p_denial[1]) * (1-p_denial[3]) if proc_n == 3  
				
*first and second procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_2 = p_denial[1] * p_denial[2] * (1-p_denial[3]) if proc_n == 3 
							
*third procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_3 = p_denial[3] * (1-p_denial[2]) * (1-p_denial[1]) if proc_n == 3 

*first and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_3 = p_denial[1] * p_denial[3] * (1-p_denial[2]) if proc_n == 3 
			
*second and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_2_3 = p_denial[2] * p_denial[3] * (1-p_denial[1]) if proc_n == 3 

*first, second and third procedure code for a visit		
sort visit_billing_id proc_id
by visit_billing_id: replace S_1_2_3 = p_denial[1] * p_denial[2] * p_denial[3] if proc_n == 3


*for 2 line items in a visit
* no denial in a visit 
sort visit_billing_id proc_id
by visit_billing_id: replace S_no_denial = (1-p_denial[1]) * (1-p_denial[2]) if proc_n == 2

*first procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_1 = p_denial[1] * (1-p_denial[2]) if proc_n == 2 

*second procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_2 = p_denial[2] * (1-p_denial[1]) if proc_n == 2 
				
*first and second procedure code for a visit	
sort visit_billing_id proc_id	
by visit_billing_id: replace S_1_2 = p_denial[1] * p_denial[2] if proc_n == 2  
						 

*for 1 line items in a visit
* no denial in a visit 
sort visit_billing_id proc_id
by visit_billing_id: replace S_no_denial = (1-p_denial) if proc_n == 1

*first procedure code for a visit
sort visit_billing_id proc_id
by visit_billing_id: replace S_1 = p_denial if proc_n == 1 
				 


*Create combinations of all line item within a visit and generate number of line items for the subset, value of the subset and choice variable 
forval s = 1/5 {    		 

	if `s' == 1 {
	
		sort visit_billing_id proc_id
				
		by visit_billing_id: gen amt_`s' = line_item_value[`s']
		
		by visit_billing_id: gen choice_`s' = R_1[`s']
			
		gen n_`s' = 1

		forval t = 2/5 {
			
			sort visit_billing_id proc_id
			
			by visit_billing_id: gegen amt_`s'_`t' = sum(line_item_value) if inlist(proc_id,`s', `t')
			sort visit_billing_id amt_`s'_`t'
			by visit_billing_id: replace amt_`s'_`t' = amt_`s'_`t'[_n-1] if amt_`s'_`t' == . 

			by visit_billing_id: gegen choice_`s'_`t' = sum(R_1) if inlist(proc_id,`s', `t')
			sort visit_billing_id choice_`s'_`t'
			by visit_billing_id: replace choice_`s'_`t' = choice_`s'_`t'[_n-1] if choice_`s'_`t' == . 
					
			gen n_`s'_`t' = 2
			
			if `t' == 2 {
			
				forval u = 3/5 {
					
					sort visit_billing_id proc_id
					
					by visit_billing_id: gegen amt_`s'_`t'_`u' = sum(line_item_value) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id amt_`s'_`t'_`u'
					by visit_billing_id: replace amt_`s'_`t'_`u' = amt_`s'_`t'_`u'[_n-1] if amt_`s'_`t'_`u' == .
					
					by visit_billing_id: gegen choice_`s'_`t'_`u' = sum(R_1) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id choice_`s'_`t'_`u'
					by visit_billing_id: replace choice_`s'_`t'_`u' = choice_`s'_`t'_`u'[_n-1] if choice_`s'_`t'_`u' == .
					
					gen n_`s'_`t'_`u' = 3
					
					if `u' == 3 {
			
						forval v = 4/5 {
							
							sort visit_billing_id proc_id
							
							by visit_billing_id: gegen amt_`s'_`t'_`u'_`v' = sum(line_item_value) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id amt_`s'_`t'_`u'_`v'
							by visit_billing_id: replace amt_`s'_`t'_`u'_`v' = amt_`s'_`t'_`u'_`v'[_n-1] if amt_`s'_`t'_`u'_`v' == .
							
							by visit_billing_id: gegen choice_`s'_`t'_`u'_`v' = sum(R_1) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id choice_`s'_`t'_`u'_`v'
							by visit_billing_id: replace choice_`s'_`t'_`u'_`v' = choice_`s'_`t'_`u'_`v'[_n-1] if choice_`s'_`t'_`u'_`v' == .
					
							gen n_`s'_`t'_`u'_`v' = 4
							
							if `v' == 4 {
			
								forval w = 5/5 {
									
									sort visit_billing_id proc_id
									
									by visit_billing_id: gegen amt_`s'_`t'_`u'_`v'_`w' = sum(line_item_value) if inlist(proc_id,`s', `t', `u', `v', `w')
									sort visit_billing_id amt_`s'_`t'_`u'_`v'_`w'
									by visit_billing_id: replace amt_`s'_`t'_`u'_`v'_`w' = amt_`s'_`t'_`u'_`v'_`w'[_n-1] if amt_`s'_`t'_`u'_`v'_`w' == .
									
									by visit_billing_id: gegen choice_`s'_`t'_`u'_`v'_`w' = sum(R_1) if inlist(proc_id,`s', `t', `u', `v', `w')
									sort visit_billing_id choice_`s'_`t'_`u'_`v'_`w'
									by visit_billing_id: replace choice_`s'_`t'_`u'_`v'_`w' = choice_`s'_`t'_`u'_`v'_`w'[_n-1] if choice_`s'_`t'_`u'_`v'_`w' == .
							
									gen n_`s'_`t'_`u'_`v'_`w' = 5
							
								}
							}
						
						}
						
					}
						
					if `u' == 4 {
			
						forval v = 5/5 {
								
							sort visit_billing_id proc_id
								
							by visit_billing_id: gegen amt_`s'_`t'_`u'_`v' = sum(line_item_value) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id amt_`s'_`t'_`u'_`v'
							by visit_billing_id: replace amt_`s'_`t'_`u'_`v' = amt_`s'_`t'_`u'_`v'[_n-1] if amt_`s'_`t'_`u'_`v' == .
							
							by visit_billing_id: gegen choice_`s'_`t'_`u'_`v' = sum(R_1) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id choice_`s'_`t'_`u'_`v'
							by visit_billing_id: replace choice_`s'_`t'_`u'_`v' = choice_`s'_`t'_`u'_`v'[_n-1] if choice_`s'_`t'_`u'_`v' == .
							
							gen n_`s'_`t'_`u'_`v' = 4
								
						}
								
					}
				}	
			
			}
			
			
			if `t' == 3 {
			
				forval u = 4/5 {
					
					sort visit_billing_id proc_id
					
					by visit_billing_id: gegen amt_`s'_`t'_`u' = sum(line_item_value) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id amt_`s'_`t'_`u'
					by visit_billing_id: replace amt_`s'_`t'_`u' = amt_`s'_`t'_`u'[_n-1] if amt_`s'_`t'_`u' == .
					
					by visit_billing_id: gegen choice_`s'_`t'_`u' = sum(R_1) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id choice_`s'_`t'_`u'
					by visit_billing_id: replace choice_`s'_`t'_`u' = choice_`s'_`t'_`u'[_n-1] if choice_`s'_`t'_`u' == .
					
					gen n_`s'_`t'_`u' = 3
					
					if `u' == 4 {
			
						forval v = 5/5 {
							
							sort visit_billing_id proc_id
							
							by visit_billing_id: gegen amt_`s'_`t'_`u'_`v' = sum(line_item_value) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id amt_`s'_`t'_`u'_`v'
							by visit_billing_id: replace amt_`s'_`t'_`u'_`v' = amt_`s'_`t'_`u'_`v'[_n-1] if amt_`s'_`t'_`u'_`v' == .
							
							by visit_billing_id: gegen choice_`s'_`t'_`u'_`v' = sum(R_1) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id choice_`s'_`t'_`u'_`v'
							by visit_billing_id: replace choice_`s'_`t'_`u'_`v' = choice_`s'_`t'_`u'_`v'[_n-1] if choice_`s'_`t'_`u'_`v' == .
							
							gen n_`s'_`t'_`u'_`v' = 4
							
						}
						
					}
					
				}
				
			}
			
			if `t' == 4 {
			
				forval u = 5/5 {
					
					sort visit_billing_id proc_id
					
					by visit_billing_id: gegen amt_`s'_`t'_`u' = sum(line_item_value) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id amt_`s'_`t'_`u'
					by visit_billing_id: replace amt_`s'_`t'_`u' = amt_`s'_`t'_`u'[_n-1] if amt_`s'_`t'_`u' == .
					
					by visit_billing_id: gegen choice_`s'_`t'_`u' = sum(R_1) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id choice_`s'_`t'_`u'
					by visit_billing_id: replace choice_`s'_`t'_`u' = choice_`s'_`t'_`u'[_n-1] if choice_`s'_`t'_`u' == .
					
					gen n_`s'_`t'_`u' = 3
					
				}
				
			}
			
		}
	}
	
	if `s' == 2 {
			
		sort visit_billing_id proc_id
			
		by visit_billing_id: gen amt_`s' = line_item_value[`s']
		
		by visit_billing_id: gen choice_`s' = R_1[`s'] 

		gen n_`s' = 1

		forval t = 3/5 {
			
			sort visit_billing_id proc_id
			
			by visit_billing_id: gegen amt_`s'_`t' = sum(line_item_value) if inlist(proc_id,`s', `t')
			sort visit_billing_id amt_`s'_`t'
			by visit_billing_id: replace amt_`s'_`t' = amt_`s'_`t'[_n-1] if amt_`s'_`t' == .
			
			by visit_billing_id: gegen choice_`s'_`t' = sum(R_1) if inlist(proc_id,`s', `t')
			sort visit_billing_id choice_`s'_`t'
			by visit_billing_id: replace choice_`s'_`t' = choice_`s'_`t'[_n-1] if choice_`s'_`t' == . 
			
			gen n_`s'_`t' = 2
			
			if `t' == 3 {
			
				forval u = 4/5 {
					
					sort visit_billing_id proc_id
					
					by visit_billing_id: gegen amt_`s'_`t'_`u' = sum(line_item_value) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id amt_`s'_`t'_`u'
					by visit_billing_id: replace amt_`s'_`t'_`u' = amt_`s'_`t'_`u'[_n-1] if amt_`s'_`t'_`u' == .
					
					by visit_billing_id: gegen choice_`s'_`t'_`u' = sum(R_1) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id choice_`s'_`t'_`u'
					by visit_billing_id: replace choice_`s'_`t'_`u' = choice_`s'_`t'_`u'[_n-1] if choice_`s'_`t'_`u' == .
					
					gen n_`s'_`t'_`u' = 3
					
					if `u' == 4 {
			
						forval v = 5/5 {
							
							sort visit_billing_id proc_id
							
							by visit_billing_id: gegen amt_`s'_`t'_`u'_`v' = sum(line_item_value) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id amt_`s'_`t'_`u'_`v'
							by visit_billing_id: replace amt_`s'_`t'_`u'_`v' = amt_`s'_`t'_`u'_`v'[_n-1] if amt_`s'_`t'_`u'_`v' == .
							
							by visit_billing_id: gegen choice_`s'_`t'_`u'_`v' = sum(R_1) if inlist(proc_id,`s', `t', `u', `v')
							sort visit_billing_id choice_`s'_`t'_`u'_`v'
							by visit_billing_id: replace choice_`s'_`t'_`u'_`v' = choice_`s'_`t'_`u'_`v'[_n-1] if choice_`s'_`t'_`u'_`v' == .
							
							gen n_`s'_`t'_`u'_`v' = 4
							
						}
					}	
			
				}
			
			}
			
			if `t' == 4 {
			
				forval u = 5/5 {
					
					sort visit_billing_id proc_id
					
					by visit_billing_id: gegen amt_`s'_`t'_`u' = sum(line_item_value) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id amt_`s'_`t'_`u'
					by visit_billing_id: replace amt_`s'_`t'_`u' = amt_`s'_`t'_`u'[_n-1] if amt_`s'_`t'_`u' == .
					
					by visit_billing_id: gegen choice_`s'_`t'_`u' = sum(R_1) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id choice_`s'_`t'_`u'
					by visit_billing_id: replace choice_`s'_`t'_`u' = choice_`s'_`t'_`u'[_n-1] if choice_`s'_`t'_`u' == .
					
					gen n_`s'_`t'_`u' = 3
					
				}

			}
		}
	}
	
	if `s' == 3 {
			
		sort visit_billing_id proc_id
		
		by visit_billing_id: gen amt_`s' = line_item_value[`s']

		by visit_billing_id: gen choice_`s' = R_1[`s']

		gen n_`s' = 1

		forval t = 4/5 {
			
			sort visit_billing_id proc_id
						
			by visit_billing_id: gegen amt_`s'_`t' = sum(line_item_value) if inlist(proc_id,`s', `t')
			sort visit_billing_id amt_`s'_`t'
			by visit_billing_id: replace amt_`s'_`t' = amt_`s'_`t'[_n-1] if amt_`s'_`t' == . 
			
			by visit_billing_id: gegen choice_`s'_`t' = sum(R_1) if inlist(proc_id,`s', `t')
			sort visit_billing_id choice_`s'_`t'
			by visit_billing_id: replace choice_`s'_`t' = choice_`s'_`t'[_n-1] if choice_`s'_`t' == . 
			
			gen n_`s'_`t' = 2

			if `t' == 4 {
			
				forval u = 5/5 {
					
					sort visit_billing_id proc_id
					
					by visit_billing_id: gegen amt_`s'_`t'_`u' = sum(line_item_value) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id amt_`s'_`t'_`u'
					by visit_billing_id: replace amt_`s'_`t'_`u' = amt_`s'_`t'_`u'[_n-1] if amt_`s'_`t'_`u' == . 
					
					by visit_billing_id: gegen choice_`s'_`t'_`u' = sum(R_1) if inlist(proc_id,`s', `t', `u')
					sort visit_billing_id choice_`s'_`t'_`u'
					by visit_billing_id: replace choice_`s'_`t'_`u' = choice_`s'_`t'_`u'[_n-1] if choice_`s'_`t'_`u' == . 
			
					gen n_`s'_`t'_`u' = 3
			
				}
			
			}
			
		}
	}
	
	
	if `s' == 4 {
			
		sort visit_billing_id proc_id
			
		by visit_billing_id: gen amt_`s' = line_item_value[`s']
		
		by visit_billing_id: gen choice_`s' = R_1[`s']

		gen n_`s' = 1

		forval t = 5/5 {
			
			sort visit_billing_id proc_id
			
			by visit_billing_id: gegen amt_`s'_`t' = sum(line_item_value) if inlist(proc_id,`s', `t')
			sort visit_billing_id amt_`s'_`t'
			by visit_billing_id: replace amt_`s'_`t' = amt_`s'_`t'[_n-1] if amt_`s'_`t' == . 
			
			by visit_billing_id: gegen choice_`s'_`t' = sum(R_1) if inlist(proc_id,`s', `t')
			sort visit_billing_id choice_`s'_`t'
			by visit_billing_id: replace choice_`s'_`t' = choice_`s'_`t'[_n-1] if choice_`s'_`t' == . 
			
			gen n_`s'_`t' = 2

		}
	}
	
	if `s' == 5 {
			
		sort visit_billing_id proc_id
			
		by visit_billing_id: gen amt_`s' = line_item_value[`s']
		
		by visit_billing_id: gen choice_`s' = R_1[`s']

		gen n_`s' = 1
		
	}
	
}



*bring data in long format
* i.e. if there are 3 line items denied, we have three possible sets of items that can be resubmitted and we create one observation for each possible set 
expand 2 if proc_id == 2
expand 4 if proc_id == 3
expand 8 if proc_id == 4
expand 16 if proc_id == 5 

sort visit_billing_id proc_id
by visit_billing_id: gen S = _n

gen amt_S = . 
gen n_S = . 
gen G_S = .
gen choice_S = .

replace amt_S = amt_1 if S == 1 
replace n_S = n_1 if S == 1 
replace G_S = S_1 if S == 1 
replace choice_S = choice_1 if S == 1 

replace amt_S = amt_2 if S == 2
replace n_S = n_2 if S == 2
replace G_S = S_2 if S == 2
replace choice_S = choice_2 if S == 2

replace amt_S = amt_1_2 if S == 3
replace n_S = n_1_2 if S == 3
replace G_S = S_1_2 if S == 3
replace choice_S = choice_1_2 if S == 3

replace amt_S = amt_3 if S == 4
replace n_S = n_3 if S == 4
replace G_S = S_3 if S == 4
replace choice_S = choice_3 if S == 4 

replace amt_S = amt_1_3 if S == 5
replace n_S = n_1_3 if S == 5
replace G_S = S_1_3 if S == 5
replace choice_S = choice_1_3 if S == 5

replace amt_S = amt_2_3 if S == 6
replace n_S = n_2_3 if S == 6
replace G_S = S_2_3 if S == 6
replace choice_S = choice_2_3 if S == 6

replace amt_S = amt_1_2_3 if S == 7
replace n_S = n_1_2_3 if S == 7
replace G_S = S_1_2_3 if S == 7
replace choice_S = choice_1_2_3 if S == 7

replace amt_S = amt_4 if S == 8
replace n_S = n_4 if S == 8
replace G_S = S_4 if S == 8
replace choice_S = choice_4 if S == 8

replace amt_S = amt_1_4 if S == 9
replace n_S = n_1_4 if S == 9
replace G_S = S_1_4 if S == 9
replace choice_S = choice_1_4 if S == 9

replace amt_S = amt_2_4 if S == 10
replace n_S = n_2_4 if S == 10
replace G_S = S_2_4 if S == 10
replace choice_S = choice_2_4 if S == 10

replace amt_S = amt_3_4 if S == 11
replace n_S = n_3_4 if S == 11
replace G_S = S_3_4 if S == 11
replace choice_S = choice_3_4 if S == 11

replace amt_S = amt_1_2_4 if S == 12
replace n_S = n_1_2_4 if S == 12
replace G_S = S_1_2_4 if S == 12
replace choice_S = choice_1_2_4 if S == 12

replace amt_S = amt_1_3_4 if S == 13
replace n_S = n_1_3_4 if S == 13
replace G_S = S_1_3_4 if S == 13
replace choice_S = choice_1_3_4 if S == 13

replace amt_S = amt_2_3_4 if S == 14
replace n_S = n_2_3_4 if S == 14
replace G_S = S_2_3_4 if S == 14
replace choice_S = choice_2_3_4 if S == 14

replace amt_S = amt_1_2_3_4 if S == 15
replace n_S = n_1_2_3_4 if S == 15
replace G_S = S_1_2_3_4 if S == 15
replace choice_S = choice_1_2_3_4 if S == 15

replace amt_S = amt_5 if S == 16
replace n_S = n_5 if S == 16
replace G_S = S_5 if S == 16
replace choice_S = choice_5 if S == 16

replace amt_S = amt_1_5 if S == 17
replace n_S = n_1_5 if S == 17
replace G_S = S_1_5 if S == 17
replace choice_S = choice_1_5 if S == 17

replace amt_S = amt_2_5 if S == 18
replace n_S = n_2_5 if S == 18
replace G_S = S_2_5 if S == 18
replace choice_S = choice_2_5 if S == 18

replace amt_S = amt_3_5 if S == 19
replace n_S = n_3_5 if S == 19
replace G_S = S_3_5 if S == 19
replace choice_S = choice_3_5 if S == 19

replace amt_S = amt_4_5 if S == 20
replace n_S = n_4_5 if S == 20
replace G_S = S_4_5 if S == 20
replace choice_S = choice_4_5 if S == 20

replace amt_S = amt_1_2_5 if S == 21
replace n_S = n_1_2_5 if S == 21
replace G_S = S_1_2_5 if S == 21
replace choice_S = choice_1_2_5 if S == 21

replace amt_S = amt_1_3_5 if S == 22
replace n_S = n_1_3_5 if S == 22
replace G_S = S_1_3_5 if S == 22
replace choice_S = choice_1_3_5 if S == 22

replace amt_S = amt_1_4_5 if S == 23
replace n_S = n_1_4_5 if S == 23
replace G_S = S_1_4_5 if S == 23
replace choice_S = choice_1_4_5 if S == 23

replace amt_S = amt_2_3_5 if S == 24
replace n_S = n_2_3_5 if S == 24
replace G_S = S_2_3_5 if S == 24
replace choice_S = choice_2_3_5 if S == 24

replace amt_S = amt_2_4_5 if S == 25
replace n_S = n_2_4_5 if S == 25
replace G_S = S_2_4_5 if S == 25
replace choice_S = choice_2_4_5 if S == 25

replace amt_S = amt_3_4_5 if S == 26
replace n_S = n_3_4_5 if S == 26
replace G_S = S_3_4_5 if S == 26
replace choice_S = choice_3_4_5 if S == 26

replace amt_S = amt_1_2_3_5 if S == 27
replace n_S = n_1_2_3_5 if S == 27
replace G_S = S_1_2_3_5 if S == 27
replace choice_S = choice_1_2_3_5 if S == 27

replace amt_S = amt_1_2_4_5 if S == 28
replace n_S = n_1_2_4_5 if S == 28
replace G_S = S_1_2_4_5 if S == 28
replace choice_S = choice_1_2_4_5 if S == 28

replace amt_S = amt_1_3_4_5 if S == 29
replace n_S = n_1_3_4_5 if S == 29
replace G_S = S_1_3_4_5 if S == 29
replace choice_S = choice_1_3_4_5 if S == 29

replace amt_S = amt_2_3_4_5 if S == 30
replace n_S = n_2_3_4_5 if S == 30
replace G_S = S_2_3_4_5 if S == 30
replace choice_S = choice_2_3_4_5 if S == 30

replace amt_S = amt_1_2_3_4_5 if S == 31
replace n_S = n_1_2_3_4_5 if S == 31
replace G_S = S_1_2_3_4_5 if S == 31
replace choice_S = choice_1_2_3_4_5 if S == 31 
bys visit_billing_id: gegen p_S_denied = sum(G_S)
assert float(p_S_denied) <= 1 & float(p_S_denied) >= 0 




*generate subsets Q of all subsets S for all visit billing IDs
gen amt_Q = . 
gen n_Q = . 
gen G_Q = .

expand 3 if S == 3			// S == first and second procedure 
expand 3 if S == 5			// S == first and third procedure 
expand 3 if S == 6 			// S == second and third procedure 
expand 7 if S == 7			// S == first and second and third procedure 
expand 3 if S == 9 			// S == first and fourth procedure 
expand 3 if S == 10 		// S == second and fourth procedure 
expand 3 if S == 11			// S == third and fourth procedure 
expand 7 if S == 12			// S == first and second and fourth procedure 
expand 7 if S == 13 		// S == first and third and fourh procedure 
expand 7 if S == 14			// S == second and third and fourth procedure 
expand 15 if S == 15		// S == first and second and third and fourth procedure 
expand 3 if S == 17			// S == first and fifth procedure 
expand 3 if S == 18			// S == second and fifth procedure 
expand 3 if S == 19			// S == third and fifth procedure 
expand 3 if S == 20			// S == fourth and fifth procedure 
expand 7 if S == 21			// S == first and second and fifth procedure 
expand 7 if S == 22			// S == first and third and fifth procedure 
expand 7 if S == 23			// S == first and fourth and fifth procedure 
expand 7 if S == 24			// S == second and third and fifth procedure 
expand 7 if S == 25			// S == second and fourth and fifth procedure 
expand 7 if S == 26			// S == third and fourth and fifth procedure 
expand 15 if S == 27		// S == first and second and third and fifth procedure 
expand 15 if S == 28		// S == first and second and fourth and fifth procedure 
expand 15 if S == 29		// S == first and third and fourth and fifth procedure 
expand 15 if S == 30		// S == second and third and fourth and fifth procedure 
expand 31 if S == 31 		// S == first and second and third and fourth and fifth procedure
	
bys visit_billing_id S: gen Q = _n

*set of first and third procedure code
replace Q = 4 if Q == 2 & S == 5 		// third procedure code 
replace Q = 5 if Q == 3 & S == 5 		// first and third procedure code

*set of second and third procedure code
replace Q = 2 if Q == 2 & S == 6 		// second procedure code 
replace Q = 4 if Q == 1 & S == 6 		// third procedure code
replace Q = 6 if Q == 3 & S == 6 		// second and third procedure code 

*set of first and fourth procedure code
replace Q = 8 if Q == 2 & S == 9 		// fourth procedure code 
replace Q = 9 if Q == 3 & S == 9		// first and fourth procedure code 

*set of second and fourth procedure code
replace Q = 2 if Q == 2 & S == 10		// second procedure code 
replace Q = 8 if Q == 1 & S == 10		// fourth procedure code
replace Q = 10 if Q == 3 & S == 10		// second and fourth procedure code 

*set of third and fourth procedure code
replace Q = 4 if Q == 1 & S == 11		// third procedure code 
replace Q = 8 if Q == 2 & S == 11		// fourth procedure code
replace Q = 11 if Q == 3 & S == 11		// second and fourth procedure code 

*set of first, second and fourth procedure code
replace Q = 8 if Q == 4 & S == 12		// fourth procedure code
replace Q = 9 if Q == 5 & S == 12		// first and fourth procedure code 
replace Q = 10 if Q == 6 & S == 12		// second and fourth procedure code 
replace Q = 12 if Q == 7 & S == 12		// first and second and fourth procedure code 

*set of first, third and fourth procedure code
replace Q = 4 if Q == 4 & S == 13		// third procedure code
replace Q = 5 if Q == 5 & S == 13 		// first and third procedure code  
replace Q = 8 if Q == 2 & S == 13		// fourth procedure code
replace Q = 9 if Q == 3 & S == 13		// first and fourth procedure code 
replace Q = 11 if Q == 6 & S == 13		// third and fourth procedure code 
replace Q = 13 if Q == 7 & S == 13		// first and third and fourth procedure code

*set of second, third and fourth procedure code
replace Q = 2 if Q == 2 & S == 14		// second procedure code 
replace Q = 4 if Q == 4 & S == 14		// third procedure code
replace Q = 6 if Q == 6 & S == 14		// second and third procedure code 
replace Q = 8 if Q == 1 & S == 14		// fourth procedure code
replace Q = 10 if Q == 5 & S == 14		// second and fourth procedure code 
replace Q = 11 if Q == 3 & S == 14		// third and fourth procedure code 
replace Q = 14 if Q == 7 & S == 14		// second and third and fourth procedure code

*set of first and fifth procedure code
replace Q = 16 if Q == 2 & S == 17 		// fifth procedure code 
replace Q = 17 if Q == 3 & S == 17 		// first and fifth procedure code

*set of second and fifth procedure code
replace Q = 2 if Q == 2 & S == 18 		// second procedure code 
replace Q = 16 if Q == 1 & S == 18 		// fifth procedure code 
replace Q = 18 if Q == 3 & S == 18 		// second and fifth procedure code 

*set of third and fifth procedure code
replace Q = 4 if Q == 1 & S == 19		// third procedure code 
replace Q = 16 if Q == 2 & S == 19		// fifth procedure code 
replace Q = 19 if Q == 3 & S == 19 		// third and fifth procedure code 

*set of fourth and fifth procedure code
replace Q = 8 if Q == 1 & S == 20		// fourth procedure code
replace Q = 16 if Q == 2 & S == 20		// fifth procedure code 
replace Q = 20 if Q == 3 & S == 20 		// fourth and fifth procedure code 

*first and second and fifth procedure 
replace Q = 16 if Q == 4 & S == 21		// fifth procedure code
replace Q = 17 if Q == 5 & S == 21		// first and fifth procedure code 
replace Q = 18 if Q == 6 & S == 21		// second and fifth procedure code
replace Q = 21 if Q == 7 & S == 21		// first, second and fifth procedure code 

*first and third and fifth procedure 
replace Q = 4 if Q == 4 & S == 22		// third procedure code
replace Q = 5 if Q == 5 & S == 22		// first and third procedure code 
replace Q = 16 if Q == 2 & S == 22		// fifth procedure code
replace Q = 17 if Q == 3 & S == 22		// first and fifth procedure code 
replace Q = 19 if Q == 6 & S == 22		// third and fifth procedure code
replace Q = 22 if Q == 7 & S == 22		// first, third and fifth procedure code 

*first and fourth and fifth procedure 
replace Q = 8 if Q == 2 & S == 23		// fourth procedure code 
replace Q = 9 if Q == 3 & S == 23		// first and fourth procedure code 
replace Q = 16 if Q == 4 & S == 23		// fifth procedure code
replace Q = 17 if Q == 5 & S == 23		// first and fifth procedure code 
replace Q = 20 if Q == 6 & S == 23		// fourth and fifth procedure code 
replace Q = 23 if Q == 7 & S == 23		// first, second and fifth procedure code 

*second and third and fifth procedure 
replace Q = 2 if Q == 2 & S == 24		// second procedure code 
replace Q = 4 if Q == 4 & S == 24		// third procedure code
replace Q = 6 if Q == 6 & S == 24		// second and third procedure code 
replace Q = 16 if Q == 1 & S == 24		// fifth procedure code
replace Q = 18 if Q == 5 & S == 24		// second and fifth procedure code 
replace Q = 19 if Q == 3 & S == 24		// third and fifth procedure code
replace Q = 24 if Q == 7 & S == 24		// second, third and fifth procedure code

*second and fourth and fifth procedure 
replace Q = 2 if Q == 2 & S == 25		// second procedure code 
replace Q = 8 if Q == 1 & S == 25		// fourth procedure code
replace Q = 10 if Q == 3 & S == 25		// second and fourth procedure code 
replace Q = 16 if Q == 4 & S == 25		// fifth procedure code
replace Q = 18 if Q == 5 & S == 25		// second and fifth procedure code 
replace Q = 20 if Q == 6 & S == 25		// fourth and fifth procedure code
replace Q = 25 if Q == 7 & S == 25		// second, fourth and fifth procedure code

*third and fourth and fifth procedure 
replace Q = 4 if Q == 4 & S == 26		// third procedure code 
replace Q = 8 if Q == 2 & S == 26		// fourth procedure code
replace Q = 11 if Q == 3 & S == 26		// third and fourth procedure code 
replace Q = 16 if Q == 1 & S == 26		// fifth procedure code
replace Q = 19 if Q == 5 & S == 26		// third and fifth procedure code 
replace Q = 20 if Q == 6 & S == 26		// fourth and fifth procedure code
replace Q = 26 if Q == 7 & S == 26		// third, fourth and fifth procedure code

*first and second and third and fifth procedure 
replace Q = 16 if Q == 8 & S == 27		// fifth procedure code
replace Q = 17 if Q == 9 & S == 27		// first and fifth procedure code 
replace Q = 18 if Q == 10 & S == 27		// second and fifth procedure code
replace Q = 19 if Q == 11 & S == 27		// third and fifth procedure code 
replace Q = 21 if Q == 12 & S == 27		// first and second and fifth procedure code
replace Q = 22 if Q == 13 & S == 27		// first, third and fifth procedure code
replace Q = 24 if Q == 14 & S == 27		// second, third and fifth procedure code
replace Q = 27 if Q == 15 & S == 27		// first, second, third and fifth procedure code

*first and second and fourth and fifth procedure 
replace Q = 8 if Q == 8 & S == 28		// fourth procedure code 
replace Q = 9 if Q == 9 & S == 28		// first and fourth procedure code 
replace Q = 10 if Q == 10 & S == 28		// second and fourth procedure code 
replace Q = 12 if Q == 12 & S == 28		// first and second and fourth procedure code 
replace Q = 16 if Q == 4 & S == 28		// fifth procedure code
replace Q = 17 if Q == 5 & S == 28		// first and fifth procedure code 
replace Q = 18 if Q == 6 & S == 28		// second and fifth procedure code
replace Q = 20 if Q == 11 & S == 28		// fourth and fifth procedure code 
replace Q = 21 if Q == 7 & S == 28		// first and second and fifth procedure code
replace Q = 23 if Q == 13 & S == 28		// first, fourth and fifth procedure code
replace Q = 25 if Q == 14 & S == 28		// second, fourth and fifth procedure code
replace Q = 28 if Q == 15 & S == 28		// first, second, fourth and fifth procedure code

*first and third and fourth and fifth procedure 
replace Q = 4 if Q == 4 & S == 29		// third procedure code
replace Q = 5 if Q == 5 & S == 29		// first and third procedure code 
replace Q = 8 if Q == 8 & S == 29		// fourth procedure code 
replace Q = 9 if Q == 9 & S == 29		// first and fourth procedure code 
replace Q = 11 if Q == 11 & S == 29		// third and fourth procedure code 
replace Q = 13 if Q == 13 & S == 29		// first and third and fourth procedure code 
replace Q = 16 if Q == 2 & S == 29		// fifth procedure code
replace Q = 17 if Q == 3 & S == 29		// first and fifth procedure code 
replace Q = 19 if Q == 6 & S == 29		// third and fifth procedure code
replace Q = 20 if Q == 7 & S == 29		// fourth and fifth procedure code 
replace Q = 22 if Q == 12 & S == 29		// first and third and fifth procedure code
replace Q = 23 if Q == 10 & S == 29		// first, fourth and fifth procedure code
replace Q = 26 if Q == 14 & S == 29		// third, fourth and fifth procedure code
replace Q = 29 if Q == 15 & S == 29		// first, third, fourth and fifth procedure codes

*second and third and fourth and fifth procedure 
replace Q = 2 if Q == 2 & S == 30		// second procedure code 
replace Q = 4 if Q == 4 & S == 30		// third procedure code
replace Q = 6 if Q == 6 & S == 30		// second and third procedure code 
replace Q = 8 if Q == 8 & S == 30		// fourth procedure code 
replace Q = 10 if Q == 10 & S == 30		// second and fourth procedure code 
replace Q = 11 if Q == 11 & S == 30		// third and fourth procedure code 
replace Q = 14 if Q == 14 & S == 30		// second and third and fourth procedure code 
replace Q = 16 if Q == 1 & S == 30		// fifth procedure code
replace Q = 18 if Q == 3 & S == 30		// second and fifth procedure code 
replace Q = 19 if Q == 5 & S == 30		// third and fifth procedure code
replace Q = 20 if Q == 7 & S == 30		// fourth and fifth procedure code 
replace Q = 24 if Q == 9 & S == 30		// second and third and fifth procedure code
replace Q = 25 if Q == 12 & S == 30		// second, fourth and fifth procedure code
replace Q = 26 if Q == 13 & S == 30		// third, fourth and fifth procedure code
replace Q = 30 if Q == 15 & S == 30		// second, third, fourth and fifth procedure codes

replace amt_Q = amt_1 if Q == 1 
replace n_Q = n_1 if Q == 1 
replace G_Q = S_1 if Q == 1 

replace amt_Q = amt_2 if Q == 1 & S == 2
replace n_Q = n_2 if Q == 1 & S == 2
replace G_Q = S_2 if Q == 1 & S == 2

replace amt_Q = amt_3 if Q == 1 & S == 4
replace n_Q = n_3 if Q == 1 & S == 4
replace G_Q = S_3 if Q == 1 & S == 4

replace amt_Q = amt_4 if Q == 1 & S == 8
replace n_Q = n_4 if Q == 1 & S == 8
replace G_Q = S_4 if Q == 1 & S == 8

replace amt_Q = amt_5 if Q == 1 & S == 16
replace n_Q = n_5 if Q == 1 & S == 16
replace G_Q = S_5 if Q == 1 & S == 16

replace amt_Q = amt_2 if Q == 2
replace n_Q = n_2 if Q == 2
replace G_Q = S_2 if Q == 2

replace amt_Q = amt_1_2 if Q == 3
replace n_Q = n_1_2 if Q == 3
replace G_Q = S_1_2 if Q == 3

replace amt_Q = amt_3 if Q == 4
replace n_Q = n_3 if Q == 4
replace G_Q = S_3 if Q == 4

replace amt_Q = amt_1_3 if Q == 5
replace n_Q = n_1_3 if Q == 5
replace G_Q = S_1_3 if Q == 5

replace amt_Q = amt_2_3 if Q == 6
replace n_Q = n_2_3 if Q == 6
replace G_Q = S_2_3 if Q == 6

replace amt_Q = amt_1_2_3 if Q == 7
replace n_Q = n_1_2_3 if Q == 7
replace G_Q = S_1_2_3 if Q == 7

replace amt_Q = amt_4 if Q == 8
replace n_Q = n_4 if Q == 8
replace G_Q = S_4 if Q == 8

replace amt_Q = amt_1_4 if Q == 9
replace n_Q = n_1_4 if Q == 9
replace G_Q = S_1_4 if Q == 9

replace amt_Q = amt_2_4 if Q == 10
replace n_Q = n_2_4 if Q == 10
replace G_Q = S_2_4 if Q == 10

replace amt_Q = amt_3_4 if Q == 11
replace n_Q = n_3_4 if Q == 11
replace G_Q = S_3_4 if Q == 11

replace amt_Q = amt_1_2_4 if Q == 12
replace n_Q = n_1_2_4 if Q == 12
replace G_Q = S_1_2_4 if Q == 12

replace amt_Q = amt_1_3_4 if Q == 13
replace n_Q = n_1_3_4 if Q == 13
replace G_Q = S_1_3_4 if Q == 13

replace amt_Q = amt_2_3_4 if Q == 14
replace n_Q = n_2_3_4 if Q == 14
replace G_Q = S_2_3_4 if Q == 14

replace amt_Q = amt_1_2_3_4 if Q == 15
replace n_Q = n_1_2_3_4 if Q == 15
replace G_Q = S_1_2_3_4 if Q == 15

replace amt_Q = amt_5 if Q == 16
replace n_Q = n_5 if Q == 16
replace G_Q = S_5 if Q == 16

replace amt_Q = amt_1_5 if Q == 17
replace n_Q = n_1_5 if Q == 17
replace G_Q = S_1_5 if Q == 17

replace amt_Q = amt_2_5 if Q == 18
replace n_Q = n_2_5 if Q == 18
replace G_Q = S_2_5 if Q == 18

replace amt_Q = amt_3_5 if Q == 19
replace n_Q = n_3_5 if Q == 19
replace G_Q = S_3_5 if Q == 19

replace amt_Q = amt_4_5 if Q == 20
replace n_Q = n_4_5 if Q == 20
replace G_Q = S_4_5 if Q == 20

replace amt_Q = amt_1_2_5 if Q == 21
replace n_Q = n_1_2_5 if Q == 21
replace G_Q = S_1_2_5 if Q == 21

replace amt_Q = amt_1_3_5 if Q == 22
replace n_Q = n_1_3_5 if Q == 22
replace G_Q = S_1_3_5 if Q == 22

replace amt_Q = amt_1_4_5 if Q == 23
replace n_Q = n_1_4_5 if Q == 23
replace G_Q = S_1_4_5 if Q == 23

replace amt_Q = amt_2_3_5 if Q == 24
replace n_Q = n_2_3_5 if Q == 24
replace G_Q = S_2_3_5 if Q == 24

replace amt_Q = amt_2_4_5 if Q == 25
replace n_Q = n_2_4_5 if Q == 25
replace G_Q = S_2_4_5 if Q == 25

replace amt_Q = amt_3_4_5 if Q == 26
replace n_Q = n_3_4_5 if Q == 26
replace G_Q = S_3_4_5 if Q == 26

replace amt_Q = amt_1_2_3_5 if Q == 27
replace n_Q = n_1_2_3_5 if Q == 27
replace G_Q = S_1_2_3_5 if Q == 27

replace amt_Q = amt_1_2_4_5 if Q == 28
replace n_Q = n_1_2_4_5 if Q == 28
replace G_Q = S_1_2_4_5 if Q == 28

replace amt_Q = amt_1_3_4_5 if Q == 29
replace n_Q = n_1_3_4_5 if Q == 29
replace G_Q = S_1_3_4_5 if Q == 29

replace amt_Q = amt_2_3_4_5 if Q == 30
replace n_Q = n_2_3_4_5 if Q == 30
replace G_Q = S_2_3_4_5 if Q == 30

replace amt_Q = amt_1_2_3_4_5 if Q == 31
replace n_Q = n_1_2_3_4_5 if Q == 31
replace G_Q = S_1_2_3_4_5 if Q == 31

bys visit_billing_id S: gegen p_Q_denied = sum(G_Q)
assert float(p_Q_denied) <= 1 
assert float(p_Q_denied) >= 0 

*generate the empty set observation (i.e. if no item is denied)
bys visit_billing_id: gen count_temp = _n
expand 2 if count_temp == 1 
bys visit_billing_id count_temp: gen count_temp2 = _n
replace G_Q = S_no_denial if count_temp2 == 2 
replace amt_Q = 0 if count_temp2 == 2
replace amt_Q = 0 if count_temp2 == 2

*generate bins 
egen bin_S = cut(amt_S) , at(0(50)200)			   										// generate bins for sets S, bin claim amounts - width of bin = USD 50
egen bin_Q = cut(amt_Q) , at(0(50)200)			   										// generate bins for sets Q, bin claim amounts - width of bin = USD 50

replace bin_S = 200 if bin_S == . 
replace bin_Q = 200 if bin_Q == .

*merge in stop probabilites 
merge m:1 n_S n_Q bin_S bin_Q state pay_type_pooled visit_code_cat size_indicator using `"$output/temp/stop_probabilities_weighted.dta"' 
drop if _merge == 2
gen ind = 0
replace ind = 1 if _merge == 1
bys visit_billing_id: gegen sum_ind = sum(ind)
keep if sum_ind == 0 
drop sum_ind ind 

replace p_stop = 1 if count_temp2 == 2 // so ln(p_stop) = 0 
gen V_Q = G_Q*(amt_S-amt_Q-ln(p_stop))
gen Exp_Next_Payment_Q = G_Q*(amt_S-amt_Q)
gen probability_check = p_S_denied + S_no_denial

*workaround to prevent code crashing due to collapse command
bys visit_billing_id S: gegen V_S = sum(V_Q)
bys visit_billing_id : gegen Exp_Next_Payment = total(Exp_Next_Payment_Q)

unab vars_keep: bin_S n_S state payee_state_cd pay_type* dx1_code G_S idw_provider_id choice_S probability_check visit_code_cat p_S_denied S_no_denial size_indicator

foreach var of varlist `vars_keep' {
	
	sort visit_billing_id S
	replace `var' = `var'[_n-1] if missing(`var')
	sort visit_billing_id S
	replace `var' = `var'[_n+1] if missing(`var')
}

bys visit_billing_id S: keep if _n == 1  
keep `vars_keep' visit_billing_id S V_S Exp_Next_Payment



*generate the empty set observation for each visit 
expand 2 if S == 1 
bys visit_billing_id S: replace S = 0 if _n == 2
replace n_S = . if S == 0 
replace G_S = . if S == 0 
replace V_S = . if S == 0 
replace choice_S = 0 if S == 0 

*create the choice variable 
replace choice_S = 0 if choice_S != n_S
bys visit_billing_id: gegen max_choice = max(choice_S)	
replace max_choice = 99 if max_choice == 0	
replace choice_S = 0 if choice_S != max_choice						
replace choice_S = 1 if choice_S == max_choice
replace choice_S = 1 if S == 0 & max_choice == 99

bys visit_billing_id: egen sumc =sum(choice_S)
assert sumc == 1

drop max_choice sumc probability_check S_no_denial p_S_denied

lab var S "Indicator of subsets available for resubmission"
lab var choice_S "Indicator of subset S resbumitted"
lab var bin_S "Bin Amt. of subset S"
lab var G_S  "Probability S is denied and -S is not denied"
lab var n_S "Number if line items in subset S"
lab var Exp_Next_Payment "Expected payment in next period"
lab var V_S "Value of Subset S"

save "$output/temp/relax_IND_estimation_data_bin.dta",replace 


log close 



